package com.expressage.controller;

import com.expressage.po.*;
import com.expressage.po.Package;
import com.expressage.service.*;
import com.expressage.vo.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工控制器 */
@Controller
public class StaffController {
    /*日志*/
    Logger logger =Logger.getLogger(StaffController.class);

    @Autowired
    StaffService staffService;
    @Autowired
    CheckRecordService checkRecordService;
    @Autowired
    PackageService packageService;
    @Autowired
    SendService sendService;
    @Autowired
    SiteService siteService;
    @Autowired
    RoleService roleService;
    @Autowired
    CityAddressService cityAddressService;
    @Autowired
    StreetAddressService streetAddressService;
    @Autowired
    AreaAddressService areaAddressService;
    @Autowired
    SalaryService salaryService;
    @Autowired
    PostService postService;

    /*登录注册*/
    @RequestMapping("/tostafflogin")
    public String toLogin(){
        return "jspbefore/stafflogin";
    }

    @RequestMapping("/stafflogin")
    public String login(Staff staff, HttpSession session, HttpServletRequest request) {
        //logger.info("验证码："+captcha);
        //判断用户的验证码是否输入正确
        //String captchaSession=session.getAttribute("simpleCaptcha").toString();
        Staff currentUser = staffService.findByNamePass(staff);
        if (currentUser != null) {
            String  name= currentUser.getStaffname();
            int uid=currentUser.getStaffid();
            int rid=currentUser.getRid();
            User user=new User();
            user.setUid(uid);
            user.setName(name);
            user.setRid(rid);
            session.setAttribute("currentUser", user);
            return "jspafter/main";
        } else {
            request.setAttribute("mess", "用户名或密码错误！");
            return "jspbefore/stafflogin";
        }
    }

    @RequestMapping("/tostaffregist")
    public String toRegist(Model model){
        logger.info("进入去员工注册控制器");
        List<Site> list = siteService.findAll();
        model.addAttribute("site",list);
        List<Role> list1 = roleService.findAll();
        model.addAttribute("role",list1);
        List<CityAddress> list2=cityAddressService.findAll();
        model.addAttribute("city",list2);
        List<AreaAddressVO> list3 =areaAddressService.findAll();
        model.addAttribute("area",list3);
        List<StreetAddressVO>list4 =streetAddressService.findAll();
        model.addAttribute("street",list4);
        List<Salary> list5 = salaryService.findAll();
        model.addAttribute("salary",list5);
        return "jspbefore/staffregist";
    }

    @RequestMapping("/staffregist")
    public String Regist(Staff staff){
        logger.info("进入员工注册控制器");
        boolean flag=false;
        boolean flag1=false;
        boolean flag2=false;
        boolean flag3=false;
        boolean flag4=false;
        /*添加员工*/
        flag = staffService.add(staff);
        logger.info(flag?"添加员工成功":"添加员工失败");
        logger.debug(flag);
        /*同时添加一条考勤信息，包裹信息、接单信息*/
        List<StaffVO> list = staffService.find(staff.getStaffname());
        System.out.println("list"+list.size());
        if(list!=null) {
            for (StaffVO staffVO : list) {
                /*考勤*/
                CheckRecord checkRecord =new CheckRecord();
                Package pack = new Package();
                Post post = new Post();
                Send send = new Send();
                checkRecord.setDuty(30);
                checkRecord.setStaffid(staffVO.getStaffid());
                logger.info(checkRecord);
                flag1 = checkRecordService.add(checkRecord);
                logger.info(flag1 ? "添加考勤成功" : "添加考勤失败");
                /*包裹*/
                pack.setStaffid(staffVO.getStaffid());
                pack.setOdd("HS1000258762219");
                pack.setFid(1);
                pack.setUid(3);
                pack.setSortid(1);
                pack.setPrice(12);
                pack.setWeight(2.2);
                pack.setReid(1);
                pack.setStateid(1);
                pack.setRemark("冷冻");
                logger.info(pack);
                flag2 = packageService.addPackage(pack);
                logger.info(flag2 ? "添加包裹成功" : "添加包裹失败");
                /*接单信息*/

                post.setPid(15);
                post.setStaffid(staffVO.getStaffid());
                post.setOdd("HS1000258762219");
                logger.info(post);
                flag3 = postService.insert(post);
                logger.info(flag3 ? "添加接单成功" : "添加接单失败");
                /*派单信息*/
                send.setStaffid(staffVO.getStaffid());
                System.out.println(pack.getPid());
                send.setPid(15);
                send.setOdd(pack.getOdd());
                logger.info(send);
                flag4=sendService.insert(send);
                logger.info(flag4 ? "添加派单成功" : "添加派单失败");
            }
        }
        if(flag&&flag1&&flag3&&flag4){
            return "redirect:tostafflogin.action";
        }else {
            return "";
        }

    }

    /*列表显示*/
    @RequestMapping("/stafflist")
    public String staffList(Model model){
        logger.info("进入员工列表控制器");
        List<StaffVO> list =staffService.findAll();
        if (list!=null){
            for (StaffVO staffVO:list) {
                // System.out.println("====="+staffVO.getStaffid());
                CheckRecord checkRecord=checkRecordService.findByCheck(staffVO.getStaffid());
                //logger.info(checkRecord.getDuty());
                Salary salary = salaryService.findByid(staffVO.getSalaryid());
                /*工时工资*/
                logger.debug(salary.getSalaryday());
                logger.debug(checkRecord.getDuty());
                double day =salary.getSalaryday()*checkRecord.getDuty();
                staffVO.setSalaryday(day);
                /*绩效工资*/
                PackInfo packInfo = packageService.findByStaffid(staffVO.getStaffid());
                logger.info("+++=="+packInfo.getPrice());
                logger.info("===="+packInfo.getWeight());
                PostInfo postInfo=postService.findByStaffid(staffVO.getStaffid());
                logger.info("员工id派单数"+postInfo.getValue());
                /*总价 ÷ 重量  == 单价 *数量  * 百分比*/
                double pay =(packInfo.getPrice()/packInfo.getWeight())*postInfo.getValue()* 0.6;
                logger.debug(pay);
                staffVO.setSalarypay(salary.getSalarypay()+pay);
                staffVO.setSalaysum(salary.getSalarybase()+day+pay);
            }
        }
        model.addAttribute("stafflist",list);
        return "jspafter/stafflist";
    }
    @RequestMapping("/toaddstaff")
    public String toaddstaff(Model model){
        logger.info("去添加员工控制器");
        List<Site> list = siteService.findAll();
        model.addAttribute("site",list);
        List<Role> list1 = roleService.findAll();
        model.addAttribute("role",list1);
        List<CityAddress> list2=cityAddressService.findAll();
        model.addAttribute("city",list2);
        List<AreaAddressVO> list3 =areaAddressService.findAll();
        model.addAttribute("area",list3);
        List<StreetAddressVO>list4 =streetAddressService.findAll();
        model.addAttribute("street",list4);
        List<Salary> list5 = salaryService.findAll();
        model.addAttribute("salary",list5);
        return "jspafter/addstaff";
    }
    @RequestMapping("/addstaff")
    public String addStaff(Staff staff){
        logger.info("添加员工控制器");

        boolean flag=false;
        boolean flag1=false;
        boolean flag2=false;
        boolean flag3=false;
        boolean flag4=false;
        /*添加员工*/
        flag = staffService.add(staff);
        logger.info(flag?"添加员工成功":"添加员工失败");
        logger.debug(flag);
        /*同时添加一条考勤信息，包裹信息、接单信息*/
        List<StaffVO> list = staffService.find(staff.getStaffname());
        System.out.println("list"+list.size());
        if(list!=null) {
            for (StaffVO staffVO : list) {
                /*考勤*/
                CheckRecord checkRecord =new CheckRecord();
                Package pack = new Package();
                Post post = new Post();
                Send send = new Send();
                checkRecord.setDuty(30);
                checkRecord.setStaffid(staffVO.getStaffid());
                logger.info(checkRecord);
                flag1 = checkRecordService.add(checkRecord);
                logger.info(flag1 ? "添加考勤成功" : "添加考勤失败");
                /*包裹*/
                pack.setStaffid(staffVO.getStaffid());
                pack.setOdd("HS1000258762219");
                pack.setFid(1);
                pack.setUid(3);
                pack.setSortid(1);
                pack.setPrice(12);
                pack.setWeight(2.2);
                pack.setReid(1);
                pack.setStateid(1);
                pack.setRemark("冷冻");
                logger.info(pack);
                flag2 = packageService.addPackage(pack);
                logger.info(flag2 ? "添加包裹成功" : "添加包裹失败");
                /*接单信息*/

                post.setPid(15);
                post.setStaffid(staffVO.getStaffid());
                post.setOdd("HS1000258762219");
                logger.info(post);
                 flag3 = postService.insert(post);
                logger.info(flag3 ? "添加接单成功" : "添加接单失败");
                /*派单信息*/
                send.setStaffid(staffVO.getStaffid());
                System.out.println(pack.getPid());
                send.setPid(15);
                send.setOdd(pack.getOdd());
                logger.info(send);
                flag4=sendService.insert(send);
                logger.info(flag4 ? "添加派单成功" : "添加派单失败");
            }
        }
        if(flag&&flag1&&flag3&&flag4){
            return "redirect:stafflist.action";
        }else {
            return "";
        }
    }
    @RequestMapping("/toupdatestaff")
    public String toUpdateStaff(Model model,int staffid){
        logger.info("去修改员工控制器");
        StaffVO  staffVO=staffService.findByid(staffid);
        model.addAttribute("staffVO",staffVO);
        logger.info(staffVO.getStaffsex());
        List<Site> list = siteService.findAll();
        model.addAttribute("site",list);
        List<Role> list1 = roleService.findAll();
        model.addAttribute("role",list1);
        List<CityAddress> list2=cityAddressService.findAll();
        model.addAttribute("city",list2);
        List<AreaAddressVO> list3 =areaAddressService.findAll();
        model.addAttribute("area",list3);
        List<StreetAddressVO>list4 =streetAddressService.findAll();
        model.addAttribute("street",list4);
        List<Salary> list5 = salaryService.findAll();
        model.addAttribute("salary",list5);
        return "jspafter/editstaff";
    }
    @RequestMapping("updatestaff")
    public String updateStaff(Staff staff){
        logger.info("修改员工控制器");
        boolean flag =staffService.update(staff);
        logger.info(flag?"修改成功":"修改员工失败");
        return "redirect:stafflist.action";
    }
    @RequestMapping("/deletestaff")
    public String deleteStaff(int[] array){
        if (array.length>0){
           boolean flag = staffService.delete(array);
           logger.info(flag?"删除成功":"删除失败");
        }
        return "redirect:stafflist.action";
    }
    @RequestMapping("/activestaff")
    public String activeStaff(int staffid){
        logger.info("进入修改状态控制器");
        Staff staff=new Staff();
        staff.setRid(16);
        staff.setSiteid(16);
        staff.setStaffid(staffid);
        boolean flag = staffService.update(staff);
        logger.info(flag?"修改状态成功":"修改状态失败");
        return "redirect:stafflist.action";
    }
    @RequestMapping("/tostaffyeji")
    public String toStaffYeJi(){
        return "jspafter/staffyeji";
    }
    @RequestMapping("/staffyeji")
    public String staffYeJi(String staffname,Model model){
        List<StaffVO> list = staffService.find(staffname);
        if (list!=null){
            for (StaffVO staffVO:list) {
                /*查询员工派单集合*/
                logger.info(staffVO.getStaffid());
                PostInfo postInfo=postService.findByStaffid(staffVO.getStaffid());
                logger.info("员工id派单数"+postInfo.getValue());
                staffVO.setValue(postInfo.getValue());
                /*查询员工接单集合*/
                SendInfomess sendInfo=sendService.findByStaffid(staffVO.getStaffid());
                logger.info("员工id接单数====="+sendInfo.getValues());
                staffVO.setValues(sendInfo.getValues());
            }
        }
        model.addAttribute("list",list);
        return "jspafter/staffyejilist";
    }
    @RequestMapping("/tostaffgz")
    public String toStaffGz(){
        return "jspafter/staffgz";
    }
    @RequestMapping("/staffgz")
    public String staffGz(String staffname,Model model){
        List<StaffVO> list = staffService.find(staffname);
        /*根据快递员id查询考勤时间*/
        if (list!=null){
            for (StaffVO staffVO:list) {
               // System.out.println("====="+staffVO.getStaffid());
                CheckRecord checkRecord=checkRecordService.findByCheck(staffVO.getStaffid());
                //logger.info(checkRecord.getDuty());
                Salary salary = salaryService.findByid(staffVO.getSalaryid());
                 /*工时工资*/
                logger.debug(salary.getSalaryday());
                logger.debug(checkRecord.getDuty());
                double day =salary.getSalaryday()*checkRecord.getDuty();
                staffVO.setSalaryday(day);
                /*绩效工资*/
                PackInfo packInfo = packageService.findByStaffid(staffVO.getStaffid());
                logger.info("+++=="+packInfo.getPrice());
                logger.info("===="+packInfo.getWeight());
                PostInfo postInfo=postService.findByStaffid(staffVO.getStaffid());
                logger.info("员工id派单数"+postInfo.getValue());
                /*总价 ÷ 重量  == 单价 *数量  * 百分比*/
                double pay =(packInfo.getPrice()/packInfo.getWeight())*postInfo.getValue()* 0.6;
                logger.debug(pay);
                staffVO.setSalarypay(salary.getSalarypay()+pay);
                staffVO.setSalaysum(salary.getSalarybase()+day+pay);
            }
        }
        model.addAttribute("list",list);
        return "jspafter/staffgzlist";
    }

}




















